package Easy回文链表;

import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Solution {
    //比较慢的
    public boolean isPalindrome(ListNode head) {
        List<Integer> list = new ArrayList<>();
        while (head != null){
            list.add(head.val);
            head = head.next;
        }
        for (int i = 0 , j = list.size()-1; i < j; i++,j--) {
            if (!Objects.equals(list.get(i), list.get(j)))
                return false;
        }
        return true;
    }
    //超级慢的结题思路
    public boolean isPalindrome1(ListNode head) {
        String s = new String();
        while (head != null){
            s = s+String.valueOf(head.val);
            head = head.next;
        }
        for (int i = 0 , j = s.length()-1; i < j; i++,j--) {
            if (s.charAt(i) != s.charAt(j))
                return false;
        }
        return true;
    }
    @Test
    public void test(){
        ListNode l1 = new ListNode(1,new ListNode(2,null));
        System.out.println(isPalindrome1(l1));
    }
}
